const url = 'http://localhost:19001/readcardApi/ReadCardService/ReadCard'
const readCard = {
  doReadCard: (success, fail) => {
    var http = new XMLHttpRequest()
    var params = 'orem=ipsum&name=binny'
    try {
      http.open('GET', url, true)
    } catch (e) {
      fail('访问身份证读卡服务失败，请检查电脑配置')
      return
    }
    //Send the proper header information along with the request
    http.onreadystatechange = function () {
      //Call a function when the state changes.
      if (http.status == 200) {
        if (http.readyState == 4) {
          let cert = JSON.parse(http.responseText)
          console.log(cert)
          if (cert.Success) {
            // cert.Data.identityPic= 'data:image/jpeg;base64,'+cert.Data.identityPic
            success(cert.Data)
          } else if (cert.ErrorMsg) {
            fail(cert.ErrorMsg)
          } else {
            fail(
              cert.errorMsg +
                '，请取回身份证并重新放置，并在‘滴’一声后再点读取；\r\n如失败多次，请检查身份证阅读器连接。'
            )
          }
        }
      } else {
        fail(`读卡失败3 readyState:${http.readyState}  state:${http.status}`)
      }
    }
    http.send(params)
  },
}
export default readCard
